package com.demo2do.fms.admin.dao.supplier;

import java.util.List;

import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.persistence.annotation.Execute;
import com.demo2do.core.persistence.annotation.Limit;
import com.demo2do.core.persistence.annotation.Query;
import com.demo2do.core.persistence.annotation.Variable;
import com.demo2do.fms.entity.supplier.common.AuditStatus;
import com.demo2do.fms.entity.supplier.common.Supplier;
import com.demo2do.fms.entity.supplier.common.SupplierRelation;
import com.demo2do.fms.entity.supplier.common.Type;

/**
 *
 * @author Ark
 *
 */
@Dao("hibernate")
public interface SupplierRelationDao {
	
	/**
	 * 所属二级供方数量
	 * 
	 * @param parent
	 * @return
	 */
	@Query("SELECT COUNT(*) FROM SupplierRelation r WHERE r.parent = :parent AND r.disabled = false")
	public int countSponsoredSupplier(@Variable("parent") Supplier parent);
	
	/**
	 * 列出一级供方所属二级供方
	 * 
	 * @param parent
	 * @return
	 */
	@Query("SELECT r.supplier FROM SupplierRelation r WHERE r.parent = :parent AND r.disabled = false")
	public List<Supplier> listSponsoredSupplier(@Variable("parent") Supplier parent);
	
	/**
	 * 列出一级供方所属二级供方
	 * 
	 * @param parent
	 * @param benginIndex
	 * @param maxResult
	 * @return
	 */
	@Limit
    @Query("SELECT r.supplier FROM SupplierRelation r WHERE r.parent = :parent AND r.disabled = false")
    public List<Supplier> listSponsoredSupplier(@Variable("parent") Supplier parent, @Variable("beginIndex")int benginIndex, @Variable("maxResult")int maxResult);
	
	/**
	 * 列出一级供方所属二级供方
	 * 
	 * @param parent
	 * @param type
	 * @return
	 */
	@Query("SELECT r.supplier FROM SupplierRelation r WHERE r.parent = :parent AND r.disabled = false AND r.supplier.type = :type")
	public List<Supplier> listSponsoredSupplier(@Variable("parent") Supplier parent, @Variable("type") Type type);
	
	/**
	 * 列出一级供方所属二级供方的ID
	 * 
	 * @param parent
	 * @return
	 */
	@Query("SELECT r.supplier.id FROM SupplierRelation r WHERE r.parent = :parent AND r.disabled = false")
	public List<Long> listSponsoredSupplierId(@Variable("parent") Supplier parent);
	
	/**
	 * @param parent
	 * @param type
	 * @param status
	 * @return
	 */
	@Query("SELECT r.supplier FROM SupplierRelation r WHERE r.parent = :parent AND r.disabled = false AND r.supplier.type = :type AND r.supplier.status IN (:status)")
    public List<Supplier> listSponsoredSupplier(@Variable("parent") Supplier parent, @Variable("type") Type type, @Variable("status") List<AuditStatus> status);
	
	/**
	 * 列出二级供方对应的一级供方
	 * 
	 * @param supplier
	 * @return
	 */
	@Query("SELECT r.parent FROM SupplierRelation r WHERE r.supplier = :supplier AND r.disabled = false")
	public List<Supplier> listParentSupplier(@Variable("supplier") Supplier supplier);
	
	/**
	 * 列出二级供方的与一级供方关系
	 * 
	 * @param supplier
	 * @return
	 */
	@Query("FROM SupplierRelation WHERE supplier = :supplier AND disabled = false")
	public List<SupplierRelation> list(@Variable("supplier") Supplier supplier);
	
	/**
	 * 列出指定一级供方的关系
	 * 
	 * @param parent
	 * @return
	 */
	@Query("FROM SupplierRelation WHERE parent = :parent AND disabled = false")
	public List<SupplierRelation> listSubRelation(@Variable("parent") Supplier parent);
	
	/**
	 * 列出指定状态的所属供方
	 * 
	 * @param parent
	 * @param status
	 * @return
	 */
	@Query("SELECT COUNT(*) FROM SupplierRelation r WHERE r.parent = :parent AND r.supplier.status IN (:status) AND r.disabled = false")
	public int countSupplierByStatus(@Variable("parent") Supplier parent, @Variable("status") AuditStatus[] status);
	
	/**
	 * 列出指定状态和类型的所属供方
	 * 
	 * @param parent
	 * @param status
	 * @param type
	 * @return
	 */
	@Query("SELECT COUNT(*) FROM SupplierRelation r WHERE r.parent = :parent AND r.supplier.status IN (:status) AND r.supplier.type = :type AND r.disabled = false")
	public int countSupplierByStatus(@Variable("parent") Supplier parent, @Variable("status") AuditStatus[] status, @Variable("type") Type type);
	
	/**
	 * 列出指定状态的所属供方
	 * 
	 * @param parent
	 * @param status
	 * @param benginIndex
	 * @param maxResult
	 * @return
	 */
	@Limit
    @Query("SELECT r.supplier FROM SupplierRelation r WHERE r.parent = :parent AND r.supplier.status IN (:status) AND r.disabled = false")
    public List<Supplier> listSupplierByPage(@Variable("parent") Supplier parent, @Variable("status") AuditStatus[] status, @Variable("beginIndex")int benginIndex, @Variable("maxResult")int maxResult);
    
    /**
     * 列出指定状态和类型的所属供方
     * 
     * @param parent
     * @param type
     * @param status
     * @param benginIndex
     * @param maxResult
     * @return
     */
    @Limit
    @Query("SELECT r.supplier FROM SupplierRelation r WHERE r.parent = :parent AND r.supplier.status IN (:status) AND r.supplier.type = :type AND r.disabled = false")
    public List<Supplier> listSupplierByPage(@Variable("parent") Supplier parent, @Variable("type") Type type, @Variable("status") AuditStatus[] status, @Variable("beginIndex")int benginIndex, @Variable("maxResult")int maxResult);
	
    /**
     * @param parent
     */
    @Execute("UPDATE SupplierRelation SET disabled = true WHERE parent = :parent AND disabled = false")
    public void deleteByParent(@Variable("parent") Supplier parent);
    
    /**
     * @param supplier
     * @return
     */
    @Execute("UPDATE SupplierRelation SET disabled = true WHERE supplier = :supplier AND disabled = false")
    public void deleteByChild(@Variable("supplier") Supplier supplier);
    
}
